<template>
  <div class="element">
    <el-menu
      default-active="1-4-1"
      class="el-menu-vertical-demo"
      @open="handleOpen"
      @close="handleClose"
      :collapse="isCollapse"
      background-color="#545c64"
      text-color="#fff"
      active-text-color="#ffd04b"
    >
      <h3>{{ isCollapse ? "后台" : "通用后台管理系统" }}</h3>

      <!-- 渲染有子菜单的菜单 -->
      <el-submenu v-for="item in hasChildren" :key="item.name" index="1">
        <template slot="title">
          <i :class="`el-icon-${item.icon}`"></i>
          <span slot="title">导航一</span>
        </template>
        <!-- 子菜单 -->
        <el-menu-item-group
          v-for="subItem in item.children"
          :key="subItem.path"
        >
          <el-menu-item :index="subItem.path" @click="clickMenu(subItem)">{{
            subItem.label
          }}</el-menu-item>
        </el-menu-item-group>
      </el-submenu>

      <!-- 渲染没有子菜单的菜单 -->
      <el-menu-item
        :index="item.name"
        v-for="item in noChildren"
        :key="item.name"
        @click="clickMenu(item)"
      >
        <!-- 使用模板字符串拼接变量 -->
        <i :class="`el-icon-${item.icon}`"></i>
        <span slot="title">{{ item.label }}</span>
      </el-menu-item>

    </el-menu>
  </div>
</template>

<style lang="less" scope>
/*定义滚动条高宽及背景
 高宽分别对应横竖滚动条的尺寸*/
::-webkit-scrollbar {
  width: 16px;
  height: 16px;
  // background-color:#F5F5F5;
  background-color: rgba(84, 92, 100, 0.9);
}
/*定义滚动条轨道
 内阴影+圆角*/
::-webkit-scrollbar-track {
  -webkit-box-shadow: inset 0 0 6px rgba(0, 0, 0, 0.3);
  border-radius: 10px;
  // background-color:#F5F5F5;
  // background-color: rgba(84, 92, 100,.3);
}
/*定义滑块
 内阴影+圆角*/
::-webkit-scrollbar-thumb {
  border-radius: 10px;
  -webkit-box-shadow: inset 0 0 6px rgba(0, 0, 0, 0.3);
  background-color: #555;
}

// !上面是修改滑动条代码

// !下面是页面css
.el-menu-vertical-demo:not(.el-menu--collapse) {
  width: 200px;
  // min-height: 400px;
}

.el-menu {
  max-height: 100vh; // 控制最大高度
  border-right: 0px;
  h3 {
    color: #fff;
    text-align: center;
    line-height: 48px;
    font-size: 16px;
    font-weight: 400;
  }
}
</style>

<script>
import menu from './menu'
export default {
  data () {
    return {
      menuData: menu
    }
  },
  methods: {
    handleOpen (key, keyPath) {
      console.log(key, keyPath)
    },
    handleClose (key, keyPath) {
      console.log(key, keyPath)
    },
    // 点击进行跳转
    clickMenu (item) {
      if (this.$route.path !== item.path) this.$router.push(item.path)

      this.$store.commit('selectMenu', item)
    }
  },
  computed: {
    // 没有子菜单
    noChildren () {
      return this.menuData.filter((item) => !item.children)
    },

    // 有子菜单
    hasChildren () {
      return this.menuData.filter((item) => item.children)
    },

    isCollapse () {
      // 注意这里的tab是小仓库名
      return this.$store.state.tab.isCollapse
    }
  }
}
</script>
